package com.dxx.D201910312055.dxx;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dxx.D201910312055.R;

import static com.dxx.D201910312055.dxx.AutoService.mService;

public class FloatingWindow {


    private WindowManager mWindowManager = null;
    private WindowManager.LayoutParams params = null;
    private View windowView = null;
    private static Context context;


    //    一个不需要权限的悬浮窗，创建
    public FloatingWindow(Context c) {
        context = c;

        //类型是TYPE_TOAST，像一个普通的Android Toast一样。这样就不需要申请悬浮窗权限了。
        params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST);

        //初始化后不首先获得窗口焦点。不妨碍设备上其他部件的点击、触摸事件。
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.format = PixelFormat.RGBA_8888;
        params.x = 100;
        params.y = 100;

        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;

        //params.gravity=Gravity.BOTTOM;

//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(mService, "不需要权限的悬浮窗实现", Toast.LENGTH_LONG).show();
//            }
//        });


        mWindowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);

    }

    public void addView(View view) {
        mWindowManager.addView(view, params);
    }

    public void removeView(View view) {
        mWindowManager.removeView(view);
    }

    public void updateViewLayout(View view) {
        mWindowManager.updateViewLayout(view, params);
    }

    //一个添加的例子
    public TextView creat_View() {

        if (mWindowManager == null) {
            return null;
        }

        if (params == null) {
            return null;
        }

//        用TextView修改成小球样子然后设置点击事件添加删除操作
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);

        textView.setText("DXX");
        textView.setTextSize(10);
        textView.setHeight(100);
        textView.setWidth(100);
        GradientDrawable background0 = new GradientDrawable();
        background0.setShape(GradientDrawable.OVAL);
        background0.setStroke(10, Color.WHITE);//设置宽度为10px的红色描边
        background0.setGradientType(GradientDrawable.LINEAR_GRADIENT);//GradientDrawable.LINEAR_GRADIENT设置线性渐变，除此之外还有：GradientDrawable.SWEEP_GRADIENT（扫描式渐变），GradientDrawable.RADIAL_GRADIENT（圆形渐变）
        background0.setColors(new int[]{Color.GREEN, Color.BLUE});//增加渐变效果需要使用setColors方法来设置颜色（中间可以增加多个颜色值）

        textView.setBackground(background0);
//        textView.setTextColor(Color.RED);


        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                System.out.println(view.getVisibility());
//                int Visibility_int = 0;
//                if (Visibility_int == view.getVisibility()) {
//                    view.setVisibility(View.GONE);
//                } else {
//                    view.setVisibility(View.VISIBLE);
//                }


            }
        });


//        mWindowManager.addView(textView1, params);
        mWindowManager.addView(textView, params);

        textView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                params.x = (int) event.getRawX() - v.getMeasuredWidth() / 2;
                params.y = (int) event.getRawY() - v.getMeasuredHeight() / 2 - 25;
                mWindowManager.updateViewLayout(v, params);
//                mWindowManager.updateViewLayout(textView1, params);
                return false;
            }
        });

        return textView;
    }

    //    一个例子，可以自主修改使用
    public static WindowManager.LayoutParams getLayoutParams() {
        final WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.x = 100;
        wmParams.y = 100;

        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        // wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        // mParams.alpha = 0.8f;//窗口的透明度
        return wmParams;
    }


    /**
     * 悬浮窗 根据资源id创建一个可以随手指移动的悬浮窗
     */
    public FloatingWindow(int ressous, final WindowManager.LayoutParams wmParams) {
        mWindowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        final LayoutInflater layoutInflater = LayoutInflater.from(context.getApplicationContext());
        final View windowView = layoutInflater.inflate(ressous, null);
        mWindowManager.addView(windowView, wmParams);// 添加窗口


        View mAbstractAuto_WindowManager = (View) windowView.findViewById(ressous);


        mAbstractAuto_WindowManager.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                wmParams.x = (int) event.getRawX() - windowView.getMeasuredWidth() / 2;
                wmParams.y = (int) event.getRawY() - windowView.getMeasuredHeight() / 2 - 25;
                mWindowManager.updateViewLayout(windowView, wmParams);
                return false;
            }
        });


    }

    //对6.0以上版本请求获取权限
    public static void getFloatingWindowPower() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(context)) {
                Toast.makeText(context, "当前无权限，请授权", Toast.LENGTH_SHORT);

                context.startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName())));
//            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);

            }
        }

    }

    public void destroy() {
        mWindowManager.removeView(windowView);

    }


    public class myView extends View {
        //    public class myView extends View implements View.OnTouchListener {
        public float x;
        public float y;
        private Paint panter;

        public myView(Context context) {
            super(context);
            x = 100;
            y = 100;
            panter = new Paint();
//            this.setOnTouchListener(this);
        }

        /////////////////////////////
        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
            panter.setColor(Color.BLUE);
            canvas.drawCircle(x, y, 45, panter);

        }

        /*
                @Override
                public boolean onTouchEvent(MotionEvent event) {
                    x = event.getX();
                    y = event.getY();
                    invalidate();

                    return super.onTouchEvent(event);
                }*/


    }
}
